package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.swing.JLabel;
import javax.swing.JScrollPane;

public class ImageFrame extends JFrame {

	private JPanel contentPane;
	private int factor=10;
	private Image imageResized;
	private JLabel lblImageFrame;
	private JScrollPane scrollPane;
	


	/**
	 * Create the frame.
	 */
	public ImageFrame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(250, 30, 800, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
        ImageIcon imageIcon = new ImageIcon("/Users/ahmadmanaa/Documents/workspace/SGI/window.jpg");
        Image image = imageIcon.getImage();
        imageResized = ScaledImage(image,692,437);
		
		final JButton btnZoomIn = new JButton("Zoom In");
		btnZoomIn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				int width = imageResized.getWidth(null);
				int hight = imageResized.getHeight(null);
				imageResized = ScaledImage(imageResized,width+factor,hight+factor);
				lblImageFrame.setIcon(new ImageIcon(imageResized));
				
			}
		});
		btnZoomIn.setBounds(153, 509, 83, 29);
		contentPane.add(btnZoomIn);
		
		final JButton btnZoomOut = new JButton("Zoom Out");
		btnZoomOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				int width = imageResized.getWidth(null);
				int hight = imageResized.getHeight(null);
				imageResized = ScaledImage(imageResized,width-factor,hight-factor);
				lblImageFrame.setIcon(new ImageIcon(imageResized));
			}
		});
		btnZoomOut.setBounds(541, 509, 89, 29);
		contentPane.add(btnZoomOut);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(46, 26, 692, 437);
		contentPane.add(scrollPane);
		
		lblImageFrame = new JLabel(new ImageIcon(imageResized));
		scrollPane.setViewportView(lblImageFrame);
	
		
	}
	
	
	private Image ScaledImage(Image img , int width , int hight){
		
		BufferedImage resizedImage  = new BufferedImage(width, hight, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2D = resizedImage.createGraphics();
		//g2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
		g2D.drawImage(img, 0, 0, width, hight, null);
		g2D.dispose();
		return resizedImage;
	}
}
